<?php
/**
 * generator haseł
 *	
 * $zakres = 
 *  1 = a - z
 *	2 = A - Z
 *	3 = a - z and A - Z
 *	4 = a - z, A - Z and 0 - 9
 *	5 = a - z, A - Z, 0 - 9 and chars !#$%&()
 * 
 * przykład: generator_hasel::generuj_haslo(5,10);
 *
 */
class PasswordComponent extends Object {
	private $zakres;
	private $dlugosc_hasla;
	private $tablica_znakow;
	public $haslo;

	function __construct(){
		// domyślna długość hasła
		$this->dlugosc_hasla = 10;
		$this->zakers = 4;
		$this->haslo = "";
		// zakres znaków
		$this->tablica_znakow = PasswordComponent::generuj_tablice_znakow($this->zakres);
	}
	private function generuj_tablice_znakow($zakres){
		switch($zakres){
			case 1: 
				/* a - z */
				for($i = 0x61; $i <= 0x7A; $i++){
					$this->haslo .= chr($i);
				}
				return $this->haslo;
				break;
			case 2: 
				/* A - Z */
				for($i = 0x41; $i <= 0x5A; $i++){
					$this->haslo .= chr($i);
				}
				return $this->haslo;
				break;
			case 3: 
				/* a - z and A - Z */
				$this->haslo = PasswordComponent::generuj_tablice_znakow(1);
				$this->haslo .= PasswordComponent::generuj_tablice_znakow(2);
				return $this->haslo;
				break;
			case 4: 
				/* 0 - 9, A - Z and a - z */
				$this->haslo = PasswordComponent::generuj_tablice_znakow(3);
				for($i = 0x30; $i <= 0x39; $i++){
					// add chars 0 - 9;
					$this->haslo .= chr($i);
				}
				return $this->haslo;
				break;
			case 5:
				/* ! # $ % & ( ) */
				$this->haslo = PasswordComponent::generuj_tablice_znakow(4);
				for($i = 0x21; $i < 0x29; $i++){
					// wykluczając znaki:  " '
					if($i == 0x22 || $i == 0x27){
						continue;
					}
					$this->haslo .= chr($i);
				}
				return $this->haslo;
				break;
		}
	}
	
	/**
	 * 
	 * @param number $zakres zakles liczb od 1 do 5 
	 * @param number $dlugosc dlugosc wygenerowanego hasla
	 * @return string
	 * 
	 * 
	 */
	public function get($zakres=4,$dlugosc=10){
		$this->dlugosc_hasla = $dlugosc;
		$this->zakres = $zakres;
		$this->tablica_znakow = PasswordComponent::generuj_tablice_znakow($this->zakres);
		srand ((double) microtime() * 1000000);
		$this->haslo="";
		while(strlen($this->haslo) < $this->dlugosc_hasla){
			$this->haslo.=substr($this->tablica_znakow,(rand()%(strlen($this->tablica_znakow))),1);
		}
		return($this->haslo);
	}
}